<%--
  Created by IntelliJ IDEA.
  User: WHP
  Date: 2020/7/25
  Time: 14:25
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>仓库列表</title>

    <%--jq--%>
    <script src="${pageContext.request.contextPath}/static/jquery/jquery-3.5.1.js" type="text/javascript"></script>
    <%--    laiui 框架--%>
    <script src="${pageContext.request.contextPath}/static/layui/layui.js" type="text/javascript"
            charset="utf-8"></script>
    <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/layui/css/layui.css"/>
    <%--    bootstrap 框架--%>
    <script src="${pageContext.request.contextPath}/static/bootstrap/js/bootstrap.js" type="text/javascript"></script>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/bootstrap/css/bootstrap.css" type='text/css'>

    <%--    font-awesome 图标库--%>
    <link href="${pageContext.request.contextPath}/front/css/font-awesome.min.css" type="text/css" rel="stylesheet">
    <%--    百度地图API--%>
    <%--    <script type="text/javascript"--%>
    <%--            src="http://api.map.baidu.com/api?v=3.0&ak=lewICX42RZIlfELxB6dG81zz5W1juYc6"></script>--%>
    <%--    高德地图API--%>
    <script type="text/javascript"
            src="https://webapi.amap.com/maps?v=1.4.15&key=5702b4a78aee7e6d4704e647d45a5e5d&plugin=AMap.Autocomplete,AMap.PlaceSearch,AMap.DistrictSearch,AMap.Geolocation"></script>
    <%--    项目 css--%>
    <link href="${pageContext.request.contextPath}/front/css/style.css" rel="stylesheet" type="text/css"/>
</head>
<body>

<div class="main-layout" id='main-layout'>


    <div class="main-layout-container">
        <!--头部-->
        <div class="main-layout-header">

            <ul class="layui-nav" lay-filter="rightNav">


            </ul>
        </div>
        <!--主体内容-->

        <table class="table table-striped table-dark">
            <thead>
            <tr>
                <th scope="col">仓库编号</th>
                <th scope="col">仓库名称</th>
                <th scope="col">父仓库号</th>
                <th scope="col">仓库容量</th>
                <th scope="col">地址编号</th>
                <th scope="col">地址名称</th>
                <th scope="col">行政区</th>
                <th scope="col">城市码</th>
                <th scope="col">操作</th>
            </tr>
            </thead>

            <tbody id="getHouseIdList">

            </tbody>
        </table>
        <%-- 查询所有的仓库信息--%>

        <%--修改仓库信息--%>
        <!-- 修改模态框（Modal） -->
        <div class="modal fade" id="updateModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
             aria-hidden="true" style="display:none">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                            &times;
                        </button>
                        <h4 class="modal-title" id="myModalLabel">
                            修改仓库信息
                        </h4>
                    </div>
                    <form class="form-horizontal" role="form">
                        <!-- 隐藏域：标识操作类型 -->
                        <input type="hidden" name="op" value="updateByhouseId">
                        <div class="modal-body">

                            <div class="form-group row">
                                <label for="houseId" class="col-sm-2 control-label">仓库名称</label>
                                <div class="col-sm-10">
                                    <input type="text" class="form-control" id="houseId" readonly="readonly" value=""
                                           name="houseId">
                                </div>
                            </div>
                            <div class="form-group row">
                                <label for="houseName" class="col-sm-2 control-label">仓库容量</label>
                                <div class="col-sm-10">
                                    <input type="text" class="form-control" id="houseName" name="houseName">
                                </div>
                            </div>

                        </div>

                        <div class="modal-footer">
                            <button type="button" class="btn btn-default" data-dismiss="modal">关闭
                            </button>
                            <!-- data-dismiss="modal" 关闭模态框 -->
                            <button type="button" class="btn btn-primary" id="update_button" data-dismiss="modal">
                                提交更改
                            </button>
                        </div>
                    </form>
                </div><!-- /.modal-content -->
            </div><!-- /.modal -->
        </div>


    </div>

</div>
</body>

<script type="text/javascript">

    function getHouseList() {
        $.ajax({
            async: true,
            type: "GET",
            url: "${pageContext.request.contextPath}/houseMangerController/findAllHouse",
            data: {},
            dataType: "json",
            success: function (result) {
                var content = "";
                $.each(result, function (index, houseInfo) {
                    content += " <tr> <td >" + houseInfo.houseId + "</td >\n" +
                        "        <td>" + houseInfo.houseName + "</td>\n" +
                        "        <td>" + houseInfo.parentHouse.houseId + "</td>\n" +
                        "        <td>" + houseInfo.houseLimit + "</td>\n" +
                        "        <td>" + houseInfo.address.addressId + "</td>" +
                        "        <td>" + houseInfo.address.addressName + "</td>\n" +
                        "        <td>" + houseInfo.address.district + "</td>\n" +
                        "        <td>" + houseInfo.address.adCode + "</td>\n" +
                        "<td>  <a href=\"#\" data-toggle=\"modal\" data-target=\"#updateModal\" class=\"open_update_modal\" " +
                        "data-houseId=\"'" + houseInfo.houseId + "'\"" +
                        "data-houseName=\"" + houseInfo.houseName + "\"" +
                        "data-houseLimit=\"" + houseInfo.houseLimit + "\"" +

                        "<i class=\"icon-edit\"></i>修改</a> &nbsp;&nbsp;" +
                        "<a href=\"javascript:void(0)\" class=\"delete\" data-houseId=\"'" + houseInfo.houseId + "'\"><i class=\"icon-remove\"></i>删除</a>" +
                        "</td></tr>";


                })
                $("#getHouseIdList").html(content);//显示HTML
            }

        })
    }

    /* 删除仓库信息*/
    $(document).ready(function () {

        getHouseList();

        //删除事件
        $(document).on("click", ".delete", function () {
            if (window.confirm("是否确定删除")) {
                var houseId = $(this).data("houseId");//

                $.ajax({
                    async: true,
                    url: "${pageContext.request.contextPath}/houseMangerController/deleteByHouseId",
                    type: "GET",
                    data: {
                        houseId: houseId
                    },
                    dataType: "json",
                    success: function (result, status, xhr) {
                        // 调用数据渲染的方法
                        getHouseList()
                    },
                    error: function (xhr, status, error) {
                        alert("异步请求失败!");
                    }
                });
            }
        });

        /*打开模态框填充原始数据事件*/
        $(document).on("click", ".open_update_modal", function () {

            // 获取原来的数据
            var houseId = $(this).data("houseId");
            var houseName = $(this).data("houseName");
            var houseLimit = $(this).data("houseLimit");


            // 显示在模态框中指定的位置
            $("#updateModal #houseId").val(houseId);
            $("#updateModal #houseName").val(houseName);
            $("#updateModal #houseLimit").val(houseLimit);

        });

        /* 修改*/
        $(document).on("click", "#update_button", function () {
            // 自定义一个修改对象
            var updateHouse = {

                houseName: $("#updateModal #houseName").val(),
                houseLimit: $("#updateModal #houseLimit").val()

            }
            $.ajax({
                async: true,
                url: "${pageContext.request.contextPath}/houseMangerController/updateHose",
                type: "POST",
                data: {
                    op: "updateByArticleId",
                    articleTitleKey: $("#kw").val(),
                    updateArticle: JSON.stringify(updateArticle)
                },
                dataType: "json",
                success: function (result, status, xhr) {
                    // 调用数据渲染的方法
                    showData(result);
                },
                error: function (xhr, status, error) {
                    alert("异步请求失败!");
                }
            });
        });


        // 判断session中是否有用户的登录信息
        var houseManger = "${sessionScope.houseManger}";
        var content = ""
        if (houseManger != "") {
            content = " <li class=\"layui-nav-item\"><a href=\"javascript:;\">入库查询</a></li>" +
                "<li class=\"layui-nav-item\"><a href=\"javascript:;\">出库查询</a></li>" +
                "<li class=\"layui-nav-item\"><a href=\"javascript:;\">添加仓库</a></li>" +
                "<li class=\"layui-nav-item\"><a href=\"javascript:;\">退出</a></li>" +
                "<li class=\"layui-nav-item\">根据仓库号查询：</li>" +
                "<li class=\"layui-nav-item\">" +

                "  <input type=\"text\" name=\"input_houseHouseId\" required lay-verify=\"required\" placeholder=\"请输入仓库号\" autocomplete=\"off\" class=\"layui-input\"></li>" +
                "<li class=\"layui-nav-item\">" +
                "  <button type=\"button\" id=\"start_seach\" class=\"layui-btn\">搜索</button></li>" +
                "<li class=\"layui-nav-item\">" +
                "  <button type=\"button\" id=\"clean_seach\" class=\"layui-btn layui-btn-normal\">清除搜索</button></li>";

        } else {
            // 没有登录
            content = " <li class=\"layui-nav-item\"><a href=\"${pageContext.request.contextPath}/front/houseMangerLogin.jsp\" rel=\"nofollow\">登录</a></li>\n" +
                "       <li class=\"layui-nav-item\">|</li>" +
                "       <li class=\"layui-nav-item\"><a href=\"${pageContext.request.contextPath}/front/houseMangerRegister.jsp\" rel=\"nofollow\">注册</a></li>  ";
        }

        $(".layui-nav").append(content);
    })


</script>
</html>
